package ml.coursedesign.controller;



import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ml.coursedesign.common.lang.Result;
import ml.coursedesign.common.util.SubjectExcelListener;
import ml.coursedesign.entity.*;
import ml.coursedesign.service.SelectSubjectService;
import ml.coursedesign.service.StudentService;
import ml.coursedesign.service.SubjectGroupService;
import ml.coursedesign.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/teacher")
public class SubjectController {
    @Autowired
    SubjectService subjectService;
    @Autowired
    StudentService studentService;
    @Autowired
    SubjectGroupService subjectGroupService;
    @Autowired
    SelectSubjectService selectSubjectService ;

    /**
     * 查询所有的课题
     * @return
     */
    @GetMapping("/getAllSubject")
    public Result selectAllSubject(){
        List<Subject> subjectList = subjectService.list();
        System.out.println(subjectList);
        return Result.succ(subjectList);

    }



    /**
     * 查询所有的课题选着
     * @return
     */
    @GetMapping("get/subject/selected/{className}")
    public Result  getsubjectSelectList(@PathVariable String className){
        List<Subject> subjectList = subjectService.list();
        List<SubjectSelectVo> subjectSelectVoList = new ArrayList<>();
        for (Subject s :subjectList) {
            SubjectSelectVo selectVo = new SubjectSelectVo();
            selectVo.setSubjectId(s.getSubjectId());
            selectVo.setSubjectName(s.getSubjectName());
            selectVo.setPrimaryNum(s.getPrimaryNum());
            selectVo.setSelectedNum(getseletedCountNum(s.getSubjectId(),className));
            subjectSelectVoList.add(selectVo);
        }
        return Result.succ(subjectSelectVoList);
    }
    public Integer getseletedCountNum(String sujectId,String className){
       int number= selectSubjectService.getseletedCountNum(sujectId,className);
        return number;
    }

    /**
     * 根据subjectId获取subject信息
     * @param subjectId
     * @return
     */
    @GetMapping("/get/subject/{subjectId}")
    public Result getSubject(@PathVariable String subjectId){

        Subject subject = subjectService.getById(subjectId);

        return Result.succ(subject);

    }









    /**
     * 组分页
     * @param current
     * @param limit
     * @param subjectGroup
     * @return
     */
    @PostMapping("/getAllGroup/{current}/{limit}")
    public Result getAllGroup(@PathVariable("current") String current,
                              @PathVariable("limit") String limit,
                              @RequestBody SubjectGroup subjectGroup){
        System.out.println(subjectGroup);
        List <GroupVo> subjectGroups =subjectGroupService.selectGroupPage(current,limit,subjectGroup);
        return  Result.succ(subjectGroups);
    }

    /**
     * 根据组长id得到组长名字
     * @param groupLeaderId
     * @return
     */

    @GetMapping("/get/group/leade/{groupLeaderId}")
    public Result getGroupLeaderName(@PathVariable("groupLeaderId") String groupLeaderId){
        QueryWrapper<Student> queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",groupLeaderId);
        Student student = studentService.getOne(queryWrapper);
        return Result.succ(student.getStuName());
    }

    @GetMapping("get/groupNum/{subjectid}/{classid}")
    public Result getGroupNum(@PathVariable("subjectid") String subjectid,@PathVariable("classid") String classid ){
        QueryWrapper<SelectSubject> queryWrapper = new QueryWrapper();
        queryWrapper.eq("subject_id",subjectid);
        queryWrapper.eq("course_class_id",classid);
        Map<String, Object> objectMap = selectSubjectService.getMap(queryWrapper);

         if (objectMap!=null){
             return Result.succ(objectMap.size()+1);
         }
         return Result.succ(0);
    }


}
